<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleTl()" v-if="status == '4'" v-auth="'wh:whTm:tl'">
          <Icon icon="ant-design:node-expand-outlined" /> {{ t('退料') }}
        </a-button>
        <a-button type="default" @click="handleExport()" v-if="status == '4'" v-auth="'wh:whTm:dc'">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" v-auth="'wh:whTm:dc'" v-if="status == '0'" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button
          type="primary"
          danger
          ghost
          @click="handleDeleteIds()"
          v-auth="'wh:whTm:dc'"
          v-if="status == '0'"
        >
          <Icon icon="ant-design:delete-outlined" /> {{ t('删除') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'wh:whTm:dc'" v-if="status == '0'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-button type="default" @click="handlePrint()" v-if="status == '1'" v-auth="'wh:whTm:dc'">
          <Icon icon="ant-design:printer-outlined" /> {{ t('打印货位') }}
        </a-button>
        <a-button type="default" @click="handleExport2()" v-if="status == '1'" v-auth="'wh:whTm:dc'">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
    </BasicTable>
    <PosList @register="registerPosDrawer" @success="handleSuccess" />

    <!-- 批量退料登记 -->
    <BatchTl @register="registerBatchTl" @success="handleSuccess" />
    <!-- 打印 -->
    <PrintModal @register="registerPrintModal" />

    <InputForm @register="registerDrawer" @success="handleSuccess" />

    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewswhLlwhTmLlHList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useGlobSetting } from '/@/hooks/setting';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { officeTreeData } from '/@/api/sys/office';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    whTmLlListData,
    fileDataUpload,
    barCodeCheck,
    encLl,
    whTmLlDelete,
    batchDalete,
  } from '/@/api/wh/ll/whTmLl';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import PosList from './posList.vue';
  import BatchTl from '/@/views/wh/tl/batchTl.vue';
  import PrintModal from '/@/components/print/printModal.vue';
  import InputForm from './whTmLl/form.vue';
  import FormImport from './formImport2.vue';
import { integer } from 'vue-types';


  const { ctxAdminPath } = useGlobSetting();
  const { t } = useI18n('wh.ll.whTmLlH');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('领料申请管理'),
  };

  const props = defineProps({
    status: { type: String, default: '0' },
  });

  const status = props.status;

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('存货代码'),
        field: 'basInv.cinvaddcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'cinvcode',
        component: 'Input',
      },
      {
        label: t('派工领料人'),
        field: 'llBy',
        component: 'TreeSelect',
        componentProps: {
        api: officeTreeData,
        allowClear: true,
        params: { isLoadUser: true, userIdPrefix: '' },
        canSelectParent: false,
        treeCheckable: false,
        },
      },
      {
        label: t('区域'),
        field: 'area',
        component: 'Select',
        componentProps: {
          dictType: 'wh_bcp_area',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('发料仓库'),
        field: 'cwhcode',
        component: 'Input',
      },
      {
        label: t('需求仓库'),
        field: 'xqcwhcode',
        component: 'Input',
      },
      {
        label: t('申请单号'),
        field: 'sourceId',
        component: 'Input',
      },
      {
        label: t('导出次数'),
        field: 'printQty',
        component: 'Input',
      },
      
      {
        label: t('需求时间'),
        field: 'xqDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'wh_ROW_STATUS',
          allowClear: true,
          onChange: handleSuccess,
        },
        ifShow: false,
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      key: 'inv.cinvaddcode',
      sorter: true,
      width: 140,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'basInv.cinvcode',
      key: 'inv.cinvcode',
      sorter: true,
      width: 140,
      align: 'left',
    },
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      key: 'inv.cinvaddcode',
      sorter: true,
      width: 130,
      align: 'left',
      defaultHidden: true,
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('需求数'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('区域'),
      dataIndex: 'area',
      key: 'a.area',
      sorter: true,
      width: 160,
      align: 'left',
    },
    {
      title: t('单据号'),
      dataIndex: 'djno',
      key: 'a.djno',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('申请单号'),
      dataIndex: 'sourceId',
      key: 'a.sourceId',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'asd_ROW_STATUS',
    },
    {
      title: t('导出次数'),
      dataIndex: 'printQty',
      key: 'a.printQty',
      // sorter: true,
      width: 90,
      align: 'right',
    },
    {
      title: t('构成比例'),
      dataIndex: 'frate',
      key: 'a.frate',
      sorter: true,
      width: 90,
      align: 'right',
      defaultHidden: true,
    },
    {
      title: t('实发数'),
      dataIndex: 'pickQty',
      key: 'a.pick_Qty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    
    {
      title: t('需求时间'),
      dataIndex: 'xqDate',
      key: 'a.xq_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('发料仓库'),
      dataIndex: 'cwhcode',
      key: 'a.cwhcode',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('需求仓库'),
      dataIndex: 'xqcwhcode',
      key: 'a.xqcwhcode',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('选定货位'),
      dataIndex: 'cposcode',
      key: 'a.cposcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'simple-line-icons:paper-plane',
        color: 'success',
        title: t('选择货位'),
        onClick: handlePosForm.bind(this, record),
        auth: 'wh:ll:whTmLlH:pos',
        ifShow: record.allowEdit,
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除'),
        popConfirm: {
          title: t('是否确认删除'),
          confirm: handlePosEnc.bind(this, record),
        },
        auth: 'wh:ll:whTmLlH:pos',
        ifShow: record.allowEdit,
      },
      {
        label: t('撤销'),
        color: 'error',
        title: t('撤销'),
        popConfirm: {
          title: t('是否确认撤销选择货位'),
          confirm: handleCancelEnc.bind(this, record),
        },
        auth: 'wh:ll:whTmLlH:pos',
        ifShow: record.allowEdit && record.status == '1',
      },
    ],
  };

  const [registerPosDrawer, { openDrawer }] = useDrawer();
  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerDrawer, { openDrawer: openDrawer1 }] = useDrawer();
  // 批量退料
  const [registerBatchTl, { openDrawer: openDrawerBatchTl }] = useDrawer();

  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: whTmLlListData,
    beforeFetch: (params) => {
      params['status'] = props.status;
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    ellipsis: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  async function handlePosForm(record: Recordable) {
    openDrawer(true, {
      id: record.id,
      cinvcode: record.cinvcode,
      iqty: record.iqty,
      llBy: record.llBy,
      llByName: record?.user?.userName,
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  function handleForm(record: Recordable) {
    openDrawer1(true, record);
  }

  async function handlePosEnc(record: Recordabl) {
    const res = await whTmLlDelete({ djno: record.djno });
    showMessage(res.message);
    reload();
  }

  async function handleCancelEnc(record: Recordabl) {
    const res = await encLl({ djno: record.djno });
    showMessage(res.message);
    reload();
  }

  async function handleExport() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });

      const res = await fileDataUpload({ selIds: selIds.value });
      if (res.result == 'true') {
        downloadByUrl({ url: ctxAdminPath + '/file/download/' + res.data });
      } else {
        showMessage(res.message);
      }
    } else {
      showMessage('请选择一行数据！！');
    }
  }


  async function handleExport2(){
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wh/ll/whTmLlPos/exportData',
      params: getForm().getFieldsValue(),
      target: '_self',
    });
  }

  async function handleTl() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await barCodeCheck({ selIds: selIds.value });
      if (res.result == 'true') {
        openDrawerBatchTl(true, res);
      } else {
        showMessage(res.message);
      }
    } else {
      showMessage('请选择至少一条数据！！');
    }
  }

  async function handleDeleteIds() {
    if (getSelectRows().length < 1) {
      showMessage('请选择至少一条数据！！');
      return;
    }

    const selIds = getSelectRows()
      .map((item) => item.id)
      .join(',');
    const res = await batchDalete({ selIds: selIds });
    showMessage(res.message);
    handleSuccess();
    // const res = await whTmShDeleteIds({ selIds: getSelectRows().map((item) => item.id) });
    // showMessage(res.message);
    // handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  async function handlePrint() {
    if (getSelectRows().length < 1) {
      showMessage('请选择至少一条数据！！');
      return;
    }
    const idsArr = getSelectRows().map((item) => {
      return item.djno;
    });

    let params = {
      ids: idsArr.join(','),
      title: '领料货位打印',
      height: '0.9',
      width: '0.9',
      fileName: '领料货位打印',
    };
    console.log('params', params);
    openPrintModal(true, params);
  }
</script>
